﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;

namespace Launcher
{
	public partial class Launcher : Form
	{
		private Process procTarget;

		public Launcher()
		{
			InitializeComponent();
			this.procTarget = null;
		}

		protected override void OnFormClosed(FormClosedEventArgs e)
		{
			Properties.Settings.Default.Save();
			base.OnFormClosed(e);
		}

		protected override void OnFormClosing(FormClosingEventArgs e)
		{
			if (e.CloseReason == CloseReason.UserClosing)
			{
				this.Hide();
				e.Cancel = true;
			}

			base.OnFormClosing(e);
		}

		private void trayIcon_DoubleClick(object sender, EventArgs e)
		{
			if (!this.Visible)
			{
				this.Show();
				this.WindowState = FormWindowState.Normal;
			}
			this.Activate();
		}

		private void btnBrowse_Click(object sender, EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog();
			if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			{
				this.textExcuteCmd.Text = ofd.FileName;
			}
		}

		private void trayStrip_Opening(object sender, CancelEventArgs e)
		{
			this.startToolStripMenuItem.Enabled = ((this.procTarget == null) && !String.IsNullOrEmpty(this.textExcuteCmd.Text));
			this.stopToolStripMenuItem.Enabled = (this.procTarget != null);
		}

		private void closeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		private void startToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.procTarget = new Process();
			this.procTarget.StartInfo.FileName = this.textExcuteCmd.Text;
			this.procTarget.StartInfo.Arguments = this.textExcuteArg.Text;
			this.procTarget.StartInfo.CreateNoWindow = true;
			this.procTarget.StartInfo.UseShellExecute = false;

			this.procTarget.StartInfo.RedirectStandardOutput = true;
			this.procTarget.OutputDataReceived += new DataReceivedEventHandler(procTarget_OutputDataReceived);

			this.procTarget.StartInfo.RedirectStandardError = true;
			this.procTarget.ErrorDataReceived += new DataReceivedEventHandler(procTarget_ErrorDataReceived);

			this.procTarget.Start();
			this.procTarget.BeginErrorReadLine();
			this.procTarget.BeginOutputReadLine();

			this.timerProc.Start();
		}

		void procTarget_OutputDataReceived(object sender, DataReceivedEventArgs e)
		{
			if (e.Data != null)
			{
				// TODO:
			}
		}

		void procTarget_ErrorDataReceived(object sender, DataReceivedEventArgs e)
		{
			if (e.Data != null)
			{
				// TODO:
			}
		}

		private void stopToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (this.procTarget != null)
			{
				if (!this.procTarget.HasExited)
				{
					this.procTarget.Kill();
				}
				this.procTarget.CancelErrorRead();
				this.procTarget.CancelOutputRead();
				this.procTarget.Close();
				this.procTarget = null;
			}
			this.timerProc.Stop();
		}

		private void timerProc_Tick(object sender, EventArgs e)
		{
			if (this.procTarget != null && this.procTarget.HasExited)
			{
				this.stopToolStripMenuItem_Click(sender, e);
				this.startToolStripMenuItem_Click(sender, e);
			}
		}
	}
}
